/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sea.util;

import java.util.ArrayList;
import javax.swing.JPanel;
import sea.SimHyAMainFrame;

/**
 *
 * The helper file
 * 
 * @author  Zhirun Ding
 * 
 * Created on 06/07/2012
 * 
 */

public class SimHyAHelper {
    
    //get model type by the full path of model
    public static String getModelType(String modelName) {
        //String fullPath = PublicParam.WorkingDirectory + "\\" + "modelName" + ".txt";
        if (modelName.toLowerCase().indexOf(".biopepa") >= 0) {
            return "biopepa";
        } else if (modelName.toLowerCase().indexOf(".hype") >= 0) {
            return "hype";
        } else {
            return "flat";
        }        
        //return "hype";
    }
           
    public static String getNewModelVariableName(String modelType, String modelName) {
        ArrayList<String> vs = PublicParam.SimHyACommand.getListOfModelVariables();
        
        //get variable count by Model Type
        //auto variable name:ModelType_ModelFileName_$i
        if (PublicParam.ModelName2VariableHashtable.containsKey(modelName)) {
            //this model name has exits model variable
            return PublicParam.ModelName2VariableHashtable.get(modelName).toString();
        }
        
        int vCount = 1;
        for (int i = 1; i <= vs.size(); i++) {
            if (vs.get(i - 1).indexOf(modelType + "_") == 0) {
                vCount++;
            }
        }
        //add model variable to hashtable by model name
        PublicParam.ModelName2VariableHashtable.put(modelName, modelType + "_" + modelName + "_" + vCount);
        return PublicParam.ModelName2VariableHashtable.get(modelName).toString();
    }
    
    public static String getNewDataVariableName(String operateType, String modelName) {
        ArrayList<String> vs = PublicParam.SimHyACommand.getListOfDataVariables();
        
        //get variable count by operate type(Simulate,Plot,Explore,Save)
        //auto variable name:modelName_operateType_$i
        String preVar = modelName + "_" + operateType + "_";
        int vCount = 1;
        for (int i = 1; i <= vs.size(); i++) {
            if (vs.get(i - 1).indexOf(preVar) == 0) {
                vCount++;
            }
        }
        return preVar + vCount;
    }
    
//    public static void replaceSimulateCommand(String key, String newCommand) {
//        key = "," + key;
//        int beginIndex = PublicParam.SimHyACommandExpression.indexOf(key);        
//        if (beginIndex == -1) {
//            //no match key
//            if (newCommand.length() > 0) {
//                PublicParam.SimHyACommandExpression += "," + newCommand;
//            }
//        } else {
//            //match key,this command has exists
//            int endIndex = PublicParam.SimHyACommandExpression.indexOf(",", beginIndex + 1);
//            if (endIndex == -1) endIndex = PublicParam.SimHyACommandExpression.length() - 1;//last param
//            String oldCommand = PublicParam.SimHyACommandExpression.substring(beginIndex+1, endIndex+1);
//            System.out.println(oldCommand);
//            System.out.println(newCommand);
//            PublicParam.SimHyACommandExpression = PublicParam.SimHyACommandExpression.replaceAll(oldCommand, newCommand);
//        }   
//        PublicParam.SimHyACommandExpression = PublicParam.SimHyACommandExpression.replaceAll(",,", ",");
//    }
    
//    public static boolean load(String varName, String modelName, String modelType) {                   
//        //execute command
//        String s = PublicParam.SimHyACommand.load(varName, modelName, modelType);
//        if (s.length() > 0) {                        
//            //store load command to PublicParam
//            PublicParam.SimHyACommandExpression = s;
//            //store model variable to PublicParam
//            PublicParam.CurrentModelVariableName = varName;      
//            
//            //add command to history
//            PublicParam.history.add(PublicParam.SimHyACommandExpression);
//            PublicParam.historyPos = PublicParam.history.size() - 1;
//        
//            return true;
//        }
//        return false;
//    }
    
    

    public static void executeCommand() {
        System.out.println();
        //add command to history
        PublicParam.history.add(PublicParam.SimHyACommandExpression);
        PublicParam.historyPos = PublicParam.history.size() - 1;                
        //store length of message
        SimHyAMainFrame mainFrame = ((SimHyAMainFrame)PublicParam.SimHyAMainFrame);
        int errorLength = mainFrame.getConsoleTextLength("Error");
        mainFrame.printMessage("Info", " >  " +  PublicParam.SimHyACommandExpression + "\n");
        
        Thread thread = new Thread() {
            public void run() {
                //execute command        
                Object o = PublicParam.SimHyACommand.execute(PublicParam.SimHyACommandExpression);
                if (o != null && o != PublicParam.PreviousPlotDiagram) {
                    PublicParam.PreviousPlotDiagram = o;
                    //add plot to tabs
                    ((SimHyAMainFrame)PublicParam.SimHyAMainFrame).addTab((JPanel)o);
                }                      
            }
        };

        SimHyAProgressBar.show(PublicParam.SimHyAMainFrame, thread, "Command is running.....", "", "Cancel");
         
        
        String separatorString = "--------------------------------------------------------------------------------\n";        
        mainFrame.printMessage("Info", separatorString);
        if (mainFrame.getConsoleTextLength("Error") > errorLength) {
            //has error
            mainFrame.setConsoleTextAreaFocus("Error");
        } else {
            //print separator of every command
            mainFrame.setConsoleTextAreaFocus("Info");
        }
    }        
}
